<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <div class="app-space-between">
        <div class="app-space-between-left">
          <el-form-item label="门店名称" prop="storeName">
            <el-input v-model="queryParams.storeName" placeholder="请输入门店名称" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="联系人" prop="storePerson">
            <el-input v-model="queryParams.storePerson" placeholder="请输入联系人" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </div>
        <div class="app-space-between-right">
          <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
            <el-button size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门店名称" align="center" prop="storeName" />
      <el-table-column label="门店地址" align="center" prop="storeAddress" />
      <el-table-column label="登录名" align="center" prop="storeLoginName" />

      <el-table-column label="联系人/电话" align="center">
        <template slot-scope="scope">
          <div>{{scope.row.storePerson}}</div>
          <div>{{scope.row.storePhone}}</div>
        </template>
      </el-table-column>

      <el-table-column label="营业时间" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.businessTimeEnd">
            {{scope.row.businessTimeStart}} - {{scope.row.businessTimeEnd}}
          </div>
          <div v-else>{{scope.row.businessTimeStart}}</div>
        </template>
      </el-table-column>
      <el-table-column label="微信收款账号" align="center" prop="wxAccount" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="$router.push('/store/content-data/goods/'+scope.row.storeId)">商品列表
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加和编辑 -->
    <storeEdit ref="store-edit" @success="getList"></storeEdit>
  </div>
</template>

<script>
  import {
    listPost,
    getPost,
    delPost,
    addPost,
    updatePost
  } from "@/api/system/post";

  import {
    storeList,
    storeDelete
  } from '@/api/store/content'
  import storeEdit from './edit'
  export default {
    dicts: ['sys_normal_disable'],
    components: {
      storeEdit,
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        postList: [],
        // 弹出层标题
        title: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          storePerson: undefined,
          storeName: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          postName: [{
            required: true,
            message: "岗位名称不能为空",
            trigger: "blur"
          }],
          postCode: [{
            required: true,
            message: "岗位编码不能为空",
            trigger: "blur"
          }],
          postSort: [{
            required: true,
            message: "岗位顺序不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询岗位列表 */
      getList() {
        this.loading = true;
        storeList(this.queryParams).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
      },
      // 表单重置
      reset() {
        this.form = {
          storePerson: undefined,
          storeName: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.storeId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs['store-edit'].init('add')
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        let postIds = row.storeId || this.ids
        if (Array.isArray(postIds)) postIds = postIds.toString()
        this.$refs['store-edit'].init('edit', postIds)
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.postId != undefined) {
              updatePost(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.getList();
              });
            } else {
              addPost(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let postIds = row.storeId || this.ids;
        if (Array.isArray(postIds)) postIds = postIds.toString()
        this.$modal.confirm('此操作将要删除所选数据，是否继续？').then(function() {
          return storeDelete(postIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/post/export', {
          ...this.queryParams
        }, `post_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
